*! 自动获取比特币价格数据和波动率指数并绘制一幅JavaScript图表
*! 程振兴 2018年8月5日
*! bitcoin
cap prog drop bitcoin
prog def bitcoin
	version 14.0
	di "正在下载数据······"
	qui{
		clear all
		copy "http://ccvix.com/json/bitcoin" temp.txt, replace
		infix strL v 1-20000 using temp.txt, clear
		replace v = subinstr(v, `"{"symbol": "bitcoin", "data": ["', "", .)
		split v, parse("[" "," "]")
		keep v2 v3 v4
		destring, replace
		replace v2 = v2 + tc(1 jan 1970 08:00:00)
		gen date = string(v2, "%tCCCYY-NN-DD_HH:MM:SS")
		drop v2
		replace date = substr(date, 1, 11)
		gen date1 = date(date, "YMD")
		format date1 %tdCY-N-D
		order date1
		drop date
		ren date1 date
		ren v3 price
		ren v4 VIX
		label var date "日期"
		label var price "比特币价格(美元)"
		label var VIX "比特币波动率指数"
		gen datestr = string(date, "%tdCY-N-D")
		gen pricestr = string(price, "%6.2f")
		gen VIXstr = string(VIX, "%6.4f")
		fw, s h(bitcoin)
		fw `"<!DOCTYPE html>"'
		fw `"<html>"'
		fw `"<head>"'
		fw `"  <script src="http://www.czxa.top/jslib/zc/zingchart.min.js"></script>"'
		fw `"  <script>"'
		fw `"    zingchart.MODULESDIR = "http://www.czxa.top/jslib/zc/modules/";"'
		fw `"    ZC.LICENSE = ["569d52cefae586f634c54f86dc99e6a9", "ee6b7db5b51705a13dc2339db3edaf6d"];"'
		fw `"  </script>"'
		fw `"  <style>"'
		fw `"    @import 'https://fonts.googleapis.com/css?family=Open+Sans:300';"'
		fw `"    #myChart-wrapper {"'
		fw `"      margin: auto;"'
		fw `"    }  "'
		fw `"    .zc-ref {"'
		fw `"      display: none;"'
		fw `"    }"'
		fw `"  </style>"'
		fw `"</head>"'
		fw `"<body>"'
		fw `"  <div id='myChart'><a class="zc-ref" href="https://www.zingchart.com/">Powered by ZingChart</a></div>"'
		fw `"  <script>"'
		fw `"    var dates = ["'

		forval i = 1/`=_N'{
			fw `" "`=datestr[`i']'", "'
		}

		fw `"    ];"'
		fw `"    var closes = ["'

		forval i = 1/`=_N'{
			fw `" `=pricestr[`i']', "'
		}

		fw `"    ];"'
		fw `"    var VIXs = ["'

		forval i = 1/`=_N'{
			fw `" `=VIXstr[`i']', "'
		}
		fw `"    ];"'
		fw `"    var stockState = {"'
		fw `"      current: 'Max',"'
		fw `"      dates: dates,"'
		fw `"      closes: closes,"'
		fw `"      VIXs: VIXs"'
		fw `"    };"'
		fw `"    function getCloseConfig(dates, values, current) {"'
		fw `"      return {"'
		fw `"        type: 'area',"'
		fw `"        backgroundColor: "#333","'
		fw `"        height: 500,"'
		fw `"        x: 0,"'
		fw `"        y: 0,"'
		fw `"        crosshairX: {"'
		fw `"          shared: true,"'
		fw `"          plotLabel: {"'
		fw `"            backgroundColor: "#bbb","'
		fw `"            fontColor: "#222","'
		fw `"            text: "Close: %v","'
		fw `"            fontFamily: "Open Sans","'
		fw `"            y: 0,"'
		fw `"          },"'
		fw `"          scaleLabel: {"'
		fw `"            fontColor: "#222","'
		fw `"            fontFamily: "Open Sans","'
		fw `"            backgroundColor: "#bbb","'
		fw `"          }"'
		fw `"        },"'
		fw `"        title: {"'
		fw `"          text: "比特币价格与波动率指数","'
		fw `"          fontColor: "#fff","'
		fw `"          fontFamily: 'Open Sans',"'
		fw `"          fontSize: 20,"'
		fw `"          align: 'left',"'
		fw `"          offsetX: 10"'
		fw `"        },"'
		fw `"        zoom: {"'
		fw `"          shared: true"'
		fw `"        },"'
		fw `"        plotarea: {"'
		fw `"          margin: "60 50 40 50""'
		fw `"        },"'
		fw `"        plot: {"'
		fw `"          marker: {"'
		fw `"            visible: true"'
		fw `"          }"'
		fw `"        },"'
		fw `"        tooltip: {"'
		fw `"          text: "Close: %v","'
		fw `"          backgroundColor: "#BBB","'
		fw `"          borderColor: "transparent""'
		fw `"        },"'
		fw `"        scaleY: {"'
		fw `"          guide: {"'
		fw `"            visible: true,"'
		fw `"            lineStyle: 'solid',"'
		fw `"            lineColor: "#444""'
		fw `"          },"'
		fw `"          values: "0:20000:2000","'
		fw `"          item: {"'
		fw `"            fontColor: "#ddd","'
		fw `"            fontFamily: "Open Sans""'
		fw `"          }"'
		fw `"        },"'
		fw `"        scaleX: {"'
		fw `"          guide: {"'
		fw `"            visible: true,"'
		fw `"            lineStyle: 'solid',"'
		fw `"            lineColor: "#444""'
		fw `"          },"'
		fw `"          values: dates,"'
		fw `"          zooming: {"'
		fw `"            shared: true"'
		fw `"          },"'
		fw `"          item: {"'
		fw `"            fontColor: "#ddd","'
		fw `"            fontFamily: "Open Sans""'
		fw `"          }"'
		fw `"        },"'
		fw `"        series: [{"'
		fw `"          values: values,"'
		fw `"          lineColor: "#fff","'
		fw `"          lineWidth: 1,"'
		fw `"          backgroundColor: "#909090 #313131""'
		fw `"        }],"'
		fw `"        labels: [{"'
		fw `"          x: 990,"'
		fw `"          y: 10,"'
		fw `"          id: '1W',"'
		fw `"          fontColor: (current === '1W') ? "#FFF" : "#777","'
		fw `"          fontSize: "16","'
		fw `"          fontFamily: "Open Sans","'
		fw `"          cursor: "hand","'
		fw `"          text: "1W""'
		fw `"        }, {"'
		fw `"          x: 1030,"'
		fw `"          y: 10,"'
		fw `"          id: '1M',"'
		fw `"          fontColor: (current === '1M') ? "#FFF" : "#777","'
		fw `"          fontSize: "16","'
		fw `"          fontFamily: "Open Sans","'
		fw `"          cursor: "hand","'
		fw `"          text: "1M""'
		fw `"        }, {"'
		fw `"          x: 1070,"'
		fw `"          y: 10,"'
		fw `"          id: '6M',"'
		fw `"          fontColor: (current === '6M') ? "#FFF" : "#777","'
		fw `"          fontSize: "16","'
		fw `"          fontFamily: "Open Sans","'
		fw `"          cursor: "hand","'
		fw `"          text: "6M""'
		fw `"        }, {"'
		fw `"          x: 1110,"'
		fw `"          y: 10,"'
		fw `"          id: '1Y',"'
		fw `"          fontColor: (current === '1Y') ? "#FFF" : "#777","'
		fw `"          fontSize: "16","'
		fw `"          fontFamily: "Open Sans","'
		fw `"          cursor: "hand","'
		fw `"          text: "1Y""'
		fw `"        }, {"'
		fw `"          x: 1150,"'
		fw `"          y: 10,"'
		fw `"          id: 'Max',"'
		fw `"          fontColor: (current === 'Max') ? "#FFF" : "#777","'
		fw `"          fontSize: "16","'
		fw `"          fontFamily: "Open Sans","'
		fw `"          cursor: "hand","'
		fw `"          text: "Max""'
		fw `"        }]"'
		fw `"      };"'
		fw `"    }"'
		fw `"    function getVIXConfig(dates, values) {"'
		fw `"      return {"'
		fw `"        type: 'line',"'
		fw `"        height: 150,"'
		fw `"        x: 0,"'
		fw `"        y: 500,"'
		fw `"        backgroundColor: "#333","'
		fw `"        plotarea: {"'
		fw `"          margin: "20 50 20 50""'
		fw `"        },"'
		fw `"        source: {"'
		fw `"          text: "http://ccvix.com/history/bitcoin","'
		fw `"          fontColor: "#ddd","'
		fw `"          fontFamily: "Open Sans""'
		fw `"        },"'
		fw `"        tooltip: {"'
		fw `"          visible: true,"'
		fw `"          text: "VIX: %v","'
		fw `"          fontFamily: "Open Sans","'
		fw `"          borderColor: "transparent""'
		fw `"        },"'
		fw `"        zoom: {"'
		fw `"          shared: true"'
		fw `"        },"'
		fw `"        crosshairX: {"'
		fw `"          shared: true,"'
		fw `"          scaleLabel: {"'
		fw `"            visible: false"'
		fw `"          },"'
		fw `"          plotLabel: {"'
		fw `"            fontFamily: "Open Sans","'
		fw `"            backgroundColor: "#BBB","'
		fw `"            text: "VIX: %v","'
		fw `"            y: 0"'
		fw `"          }"'
		fw `"        },"'
		fw `"        scaleX: {"'
		fw `"          visible: false,"'
		fw `"          zooming: true"'
		fw `"        },"'
		fw `"        scaleY: {"'
		fw `"          visible: true"'
		fw `"        },"'
		fw `"        series: [{"'
		fw `"          values: values,"'
		fw `"          text: "VIX","'
		fw `"          backgroundColor: "#bbb""'
		fw `"        }]"'
		fw `"      };"'
		fw `"    }"'
		fw `"    zingchart.render({"'
		fw `"      id: 'myChart',"'
		fw `"      data: {"'
		fw `"        graphset: ["'
		fw `"          getCloseConfig(stockState.dates, stockState.closes, 'Max'),"'
		fw `"          getVIXConfig(stockState.dates, stockState.VIXs)"'
		fw `"        ]"'
		fw `"      },"'
		fw `"      height: 650,"'
		fw `"      width: '100%'"'
		fw `"    });"'
		fw `"    zingchart.bind('myChart', 'label_click', function(e) {"'
		fw `"      if (stockState.current === e.labelid) {"'
		fw `"        return;"'
		fw `"      }"'
		fw `"      var windowClose = [];"'
		fw `"      var windowVIX = [];"'
		fw `"      var windowDates = [];"'
		fw `"      var cut = 0;"'
		fw `"      switch (e.labelid) {"'
		fw `"        case '1W':"'
		fw `"          cut = 5;"'
		fw `"          break;"'
		fw `"        case '1M':"'
		fw `"          cut = 20;"'
		fw `"          break;"'
		fw `"        case '6M':"'
		fw `"          cut = 130;"'
		fw `"          break;"'
		fw `"        case '1Y':"'
		fw `"          cut = 260;"'
		fw `"          break;"'
		fw `"        default:"'
		fw `"          cut = stockState.dates.length;"'
		fw `"          break;"'
		fw `"      }"'
		fw `"      windowClose = stockState.closes.slice(stockState.closes.length - cut);"'
		fw `"      windowDates = stockState.dates.slice(stockState.dates.length - cut);"'
		fw `"      windowVIX = stockState.VIXs.slice(stockState.VIXs.length - cut);"'
		fw `"      zingchart.exec('myChart', 'setdata', {"'
		fw `"        data: {"'
		fw `"          graphset: ["'
		fw `"            getCloseConfig(windowDates, windowClose, e.labelid),"'
		fw `"            getVIXConfig(windowDates, windowVIX)"'
		fw `"          ]"'
		fw `"        }"'
		fw `"      });"'
		fw `"      stockState.current = e.labelid;"'
		fw `"    });"'
		fw `"  </script>"'
		fw `"</body>"'
		fw `"</html>"'
		fw, e
		erase temp.txt
		drop datestr pricestr VIXstr
	}
	di "点击打开：" "{browse bitcoin.html:bitcoin.html}"
	noi di in yellow `"数据来源：http://ccvix.com/"'
end
